package com.longjianghuasheng.logistics.business.login

import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.longjianghuasheng.logistics.base.BaseMultiStateViewModel
import com.longjianghuasheng.logistics.data.model.bean.ApiResponseNoData
import com.longjianghuasheng.logistics.entity.LoginResponse
import com.longjianghuasheng.logistics.ext.*
import com.longjianghuasheng.logistics.network.apiService

class ForgetThePasswordViewModel : BaseMultiStateViewModel() {


    val result by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    val mobileObservableField by lazy {
        ObservableField("")
    }

    val codeObservable by lazy {
        ObservableField("")
    }

    val startCount by lazy {
        ObservableInt()
    }

    val passwordObservable by lazy {
        ObservableField("")
    }
    val confirmPasswordObservable by lazy {
        ObservableField("")
    }

    fun onGetCode() {
        val mobile = mobileObservableField.get()
        if (!checkPhone(mobile)) return
        commit({ apiService.get_code(mobile ?: "", 1) }, {
            startCount.set(60)
        })
    }


    fun password_reset() {


        val mobile = mobileObservableField.get()
        val code = codeObservable.get()
        val password = passwordObservable.get()
        val confirmPassword = confirmPasswordObservable.get()

        if (!checkPhone(mobile)) return
        if (code.isNullOrBlank()) {
            toastNormal("请输入正确的验证码")
            return
        }
        if (!checkPwd(password)) return

        if (password != confirmPassword) {
            toastNormal("两次输入的密码不一致")
            return
        }


        commit2(
            { apiService.password_reset(mobile ?: "", password ?: "", password ?: "", code) },
            result
        )

    }
}